<template>
  <div class="app-container">
    <div class="searchContianer" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}"> 
      <div class="searchLeft" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">
          
          <el-form-item label="用户名称" prop="userName" class="mb8">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          
          
          <el-form-item label="手机号" prop="mobile" class="mb8">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="客户类型" prop="signedType" class="mb8">
            <el-select v-model="queryParams.signedType" placeholder="请选择客户类型" clearable>
              <el-option
                v-for="dict in dict.type.sk_sign_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
         
         <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          
        </el-form>
      </div>
      <div class="searchRight">
        <div style="margin-bottom:20px;margin-top:5px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['vanger:customerUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" border :data="customerUserList" @selection-change="handleSelectionChange"
    	 :default-sort="defaultSort" @sort-change="handleSortChange" :height="bigTableData">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="编号" align="left" prop="code" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="用户名称" align="left" prop="userName" :show-overflow-tooltip="true" min-width="100"/>
      
      <el-table-column label="头像" align="left" prop="avatar" :show-overflow-tooltip="true" min-width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="手机号" align="left" prop="mobile" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="客户类型" align="left" prop="signedType" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_sign_type" :value="scope.row.signedType"/>
        </template>
      </el-table-column>
      <el-table-column label="客户标签" align="left" prop="customerTag" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_customer_tag" :value="scope.row.customerTag"/>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="left" prop="status" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value=1
                :inactive-value=0
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="创建时间" align="left" prop="createTime" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="操作" align="left" fixed="right" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
            @click="handleDetail(scope.row)" v-hasPermi="['vanger:customerUser:query']">查看详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['vanger:customerUser:edit']">设置客户类型</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" height="300" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="showDetail">
        
        <el-form-item label="客户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入客户名称"  readonly :disabled="true"/>
        </el-form-item>
        
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" readonly  :disabled="true"/>
        </el-form-item>
        <el-form-item label="客户类型" prop="signedType">
          <el-select v-model="form.signedType" placeholder="请选择客户类型">
            <el-option
              v-for="dict in dict.type.sk_sign_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户标签" prop="customerTag">
          <el-select v-model="form.customerTag" placeholder="请选择客户标签">
            <el-option
              v-for="dict in dict.type.sk_customer_tag"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>  
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!showDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



      <!-- 查看客户详情信息 -->
    <el-dialog :title="title" :visible.sync="openViewInfo" width="85%" append-to-body :close-on-click-modal="false"
       @close="closeDialog" class="abow_dialog">

      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="showDetail">
        <el-row>
          <el-col :span="12">
              <el-form-item label="用户名称" prop="userName">
                {{form.userName}}
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="手机号" prop="mobile">
                {{form.mobile}}
              </el-form-item>
          </el-col>
          </el-row>
          <el-row>
          <el-col :span="12">
              <el-form-item label="客户类型" prop="signedType">
                <dict-tag :options="dict.type.sk_sign_type" :value="form.signedType"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="客户标签" prop="customerTag">
                <dict-tag :options="dict.type.sk_customer_tag" :value="form.customerTag"/>
              </el-form-item>
          </el-col>
          </el-row>
          <el-row>
          <el-col :span="12">
              <el-form-item label="创建时间" prop="createTime">
                {{form.createTime}}
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="账号状态" prop="status">
                <template slot-scope="scope">
                <el-switch
                    v-model="form.status"
                    :active-value=1
                    :inactive-value=0
                    @change="handleStatusChange(form)"
                  ></el-switch>
                </template>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-card>
            <el-tabs v-model="activeName">
              <el-tab-pane label="门店信息" name="store">
                  <el-table ref="tables" v-loading="loading" border :data="customerStoreList">
                    <el-table-column label="门店名称" align="left" prop="name" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="门店类型" align="left" prop="storeType" :show-overflow-tooltip="true" width="100">
                      <template slot-scope="scope">
                        <dict-tag :options="dict.type.sk_store_type" :value="scope.row.storeType"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="详细地址" align="left" prop="provinceName,cityName,districtName,address" :show-overflow-tooltip="true" min-width="100">
                      <template slot-scope="scope">
                            {{scope.row.provinceName}}{{scope.row.cityName}}{{scope.row.districtName}}{{scope.row.address}}
                      </template>
                    </el-table-column>          
                    <el-table-column label="负责人" align="left" prop="director" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="联系方式" align="left" prop="directorMobile" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="产品数量" align="left" prop="productNum" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="创建时间" align="left" prop="createTime" :show-overflow-tooltip="true" min-width="100"/>
                    </el-table-column>
                  </el-table>
              </el-tab-pane>
              <el-tab-pane label="产品信息" name="product">
                  <el-table ref="tables" v-loading="loading" border :data="customerProductList">
                    <el-table-column label="产品类型" align="left" prop="productCategoryName" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="产品名称" align="left" prop="productName" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="产品型号" align="left" prop="modelNo" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="产品序列号" align="left" prop="serialNo" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="出厂日期" align="left" prop="factoryDate" width="160">
                      <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.factoryDate, '{y}-{m}-{d} {h}:{i}') }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="质保期至" align="left" prop="expireDate" width="160">
                      <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d} {h}:{i}') }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="在保状态" align="left" prop="inServer" :show-overflow-tooltip="true" width="100">
                      <template slot-scope="scope">
                        <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.inServer"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="所属门店" align="left" prop="storeName" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="创建时间" align="left" prop="createTime" :show-overflow-tooltip="true" min-width="100"/>
                    </el-table-column>
                  </el-table>
              </el-tab-pane>
              <el-tab-pane label="地址信息" name="address">
                  <el-table ref="tables" v-loading="loading" border :data="customerAddressList">
                    <el-table-column label="联系人" align="left" prop="contactName" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="手机号" align="left" prop="contactMobile" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="所在地区" align="left" prop="provinceName" :show-overflow-tooltip="true" min-width="100">
                        <template slot-scope="scope">
                        <span> {{scope.row.provinceName}}{{scope.row.cityName}}{{scope.row.districtName}}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="详细地址" align="left" prop="address" :show-overflow-tooltip="true" min-width="100"/>
                    <el-table-column label="默认" align="left" prop="isDefault" :show-overflow-tooltip="true" width="100">
                      <template slot-scope="scope">
                        <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isDefault"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="创建时间" align="left" prop="createTime" :show-overflow-tooltip="true" min-width="100"/>
                  </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listCustomerUser, listUnion, getCustomerUser, getInfoUnion, delCustomerUser, addCustomerUser, updateCustomerUser, exportCustomerUser,changeStatus } from "@/api/vanger/customerUser";

export default {
  name: "CustomerUser",
  dicts: ['sk_common_status', 'sk_sign_type', 'sk_customer_tag','sk_yes_no','sk_store_type'],
  data() {
    return {
      // 选中选项卡的 name
      activeName: "store",
      // 表格高度
      bigTableData:'',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 客户表格数据
      customerUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      openViewInfo: false,
      // 日期范围
      dateRange: [],
      // 查看详情
      showDetail: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        params: {},
        pageNum: 1,
        pageSize: 10,
        code: null,
        userName: null,
        realName: null,
        password: null,
        avatar: null,
        mobile: null,
        signedType: null,
        customerTag: null,
        status: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        signedType: [
            { required: true }
          ],
        customerTag: [
          { required: true }
        ]
      },


      customerStoreList: [],
      customerProductList: [],
      customerAddressList: []

    };
  },
  created() {
    this.handleSortChange(this.defaultSort);
  },
  mounted() {
    this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
  },
  watch: {
    showSearch() {
      this.$nextTick(function () {
        this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
      })
    }
  },
  methods: {
  
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomerUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.customerUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.closeDialog()
    },
	// 关闭时设置
    closeDialog() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        userName: null,
        realName: null,
        password: null,
        avatar: null,
        mobile: null,
        signedType: null,
        customerTag: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null,
        customerStoreList: [],
        customerProductList: [],
        customerAddressList: []
      };
      this.resetForm("form");
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.dateRange = [];
      this.resetForm("queryForm");
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },

    // 用户状态修改
    handleStatusChange1(row) {
      let text = row.status === 1 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },


    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === 1 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "设置客户类型";
      });
    },
    
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerUser(id).then(response => {
        this.form = response.data;
        this.openViewInfo = true;
        this.title = "基本信息";
        this.showDetail = false;
        this.customerStoreList = response.data.stores;
        this.customerProductList = response.data.products;
        this.customerAddressList = response.data.addresss;
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomerUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function() {
        return delCustomerUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vanger/customerUser/export', {
        ...this.queryParams
      }, `customerUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
